<?php
/**
 * 《编号管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Code.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/CustomerUtil.class.php');

class Code extends CustomerUtil {
	public $tbl = Constant::tbl_customer_code;

	public $status = array(
		'-' => '请选择',
		'1' => '未分配',
		'2' => '已分配',
		'3' => '保留',
	
		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'code' => '编号',
			
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'customer/code/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$code_s = $request['code_s'];

		$where = " WHERE 1=1";

		if (!empty($code_s)) {
			$where .= " AND code = '" . $code_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'customer/code/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'customer/code/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$category = $request['category'];
		$code = $request['code'];
		$name = $request['name'];

		$orderby = $request['orderby'];

		$status = $request['status'];

		clean("code_" . $category . "_status_" . $status);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(category, code, name, orderby";
		$fmt .= ", status, createtime, modifytime) VALUES('%s', '%s', '%s'";
		$fmt .= ", '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $category, $code, $name, $orderby, $status);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/code/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$code = $request['code'];

		$status = $request['status'];

		clean("code_" . $category . "_status_" . $status);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET code = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/code/edit');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		require_once(dirname(__FILE__) . '/../contact/Customer.class.php');
		$customer = new Customer();
		
		if ($customer->_exist($row['code'])) {
			$row['e'] = 'E';
		} else {
			$row['e'] = '';
		}
	
		$row = $this->_view_t($row);
	
		return $row;
	}
	
	function z($controller, $request) {

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(code, status, createtime) VALUES('%s', '%s', now())";
		$count = 2000;
		while ($count < 9999) {
			$count++;
			
			if (preg_match("/(1|4)/i", $count)) {
				continue;
			}
			
			$code = sprintf("TM%s", $count);
			if ($this->exist($code)) {
				$status = '2';
			} else {
				$status = '1';
			}
			
			$sql = sprintf($fmt, $this->tbl, $code, $status);
			echo $sql . "\n";
			$db->query($sql);
		}
		
		logdate($controller, $request);
		return true;
	}
	
	function u($controller, $request) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE status = 1";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {			
			preg_match("/(\d+)/i", $row['code'], $m);
			
			if ($this->_aa($m[1])) {
				echo "S";
				continue;
			}
			
			$fmt = "UPDATE %s SET status = '2' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}
		
		logdate($controller, $request);
		return true;
	}
	
	function _aa($num) {
		$code = sprintf("GM%s", $num);
		if ($this->exist($code)) {
			return false;
		}
		
		$code = sprintf("T%s", $num);
		if ($this->exist($code)) {
			return false;
		}
		
		$code = sprintf("TM%s", $num);
		if ($this->exist($code)) {
			return false;
		}
		
		return true;
	}
	
	function exist($code) {
		$db = newdb($this->dsn_r);		
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, Constant::tbl_customer, $code);
		// echo $sql . "\n";
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return true;
		}
		
		return false;
	}
	
	function get() {
		$db = newdb($this->dsn_w);		
		$fmt = "SELECT * FROM %s WHERE status = '1' LIMIT 1";
		$sql = sprintf($fmt, $this->tbl);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET status = '2', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $row['id']);
			$db->query($sql);
			
			return $row['code'];
		}
		
		return '';
	}
	
	function t($controller, $request) {
		$code = "TM3821";
		preg_match("/(\d+)/i", $code, $m);
		print_r($m);
		
		logdate($controller, $request);
		return true;
	}
}
?>
